*Inferring the environmental protection costs by enterprises*

use "master_file_with_fuels_noemissions.dta",clear 
duplicates drop bidnr year , force
save "test_11.dta", replace


use "new_miljoskydd_stack.dta",clear 

*Aggregating costs*

egen total_du= rowtotal(extdukost intdukost)
label var total_du "sum of running costs"
egen total_other=rowtotal(madmkost foukost ovrkost)
label var total_other "sum of other costs"
egen total_investment=rowtotal(invftot invbtot)
label var total_investment "sum of investments"

egen total_pace=rowtotal(total_du total_other total_investment)

gen pace_opx=total_du+total_other
rename total_investment pace_capex
label var total_pace "Sum of PACE Components"


merge 1:1 bidnr year using "test_11.dta"

drop if _merge==1
drop _merge

*Per employee figure: first try*

g env_cost_empl=total_pace/antanst  /*per employee*/

*aggregate up to sni2007 4d:
bysort sni2007_4d year: egen sni2007_4d_total_pace=sum(total_pace)


merge 1:1 bidnr year using  "Investeringsenkaten.dta" 

drop if _merge==2
drop _merge

bysort sni2007_4d year: egen sni2007_4d_total_pbygg=sum(PBygg)
bysort sni2007_4d year: egen sni2007_4d_total_pmask=sum(PMask)
bysort sni2007_4d year: egen sni2007_4d_total_pbost=sum(PBost)
bysort sni2007_4d year: egen sni2007_4d_total_ibygg=sum(IBygg)
bysort sni2007_4d year: egen sni2007_4d_total_imask=sum(IMask)
bysort sni2007_4d year: egen sni2007_4d_total_ibost=sum(IBost)



save "master_file_with_fuels_noemissions_w_inv.dta" , replace

*Same for the emission_req file


use "master_file_with_fuels_emissions.dta",clear 
duplicates drop bidnr year , force
save "test_22.dta", replace

use "new_miljoskydd_stack.dta",clear 

*Aggregating variables*

egen total_du= rowtotal(extdukost intdukost)
label var total_du "sum of running costs"
egen total_other=rowtotal(madmkost foukost ovrkost)
label var total_other "sum of other costs"
egen total_investment=rowtotal(invftot invbtot)
label var total_investment "sum of investments"

egen total_pace=rowtotal(total_du total_other total_investment)

gen pace_opx=total_du+total_other
rename total_investment pace_capex
label var total_pace "Sum of PACE Components"

merge 1:1 bidnr year using "test_22.dta"


drop if _merge==1
drop _merge

*Per employee figure: *

g env_cost_empl=total_pace/antanst  /*per employee*/

*aggregate up to sni2007 4d:
bysort sni2007_4d year: egen sni2007_4d_total_pace=sum(total_pace)


merge 1:1 bidnr year using  "Investeringsenkaten.dta" 

drop if _merge==2
drop _merge


bysort sni2007_4d year: egen sni2007_4d_total_pbygg=sum(PBygg)
bysort sni2007_4d year: egen sni2007_4d_total_pmask=sum(PMask)
bysort sni2007_4d year: egen sni2007_4d_total_pbost=sum(PBost)
bysort sni2007_4d year: egen sni2007_4d_total_ibygg=sum(IBygg)
bysort sni2007_4d year: egen sni2007_4d_total_imask=sum(IMask)
bysort sni2007_4d year: egen sni2007_4d_total_ibost=sum(IBost)



save "master_file_with_fuels_emissions_w_inv.dta", replace



